package com.example.androidshixi;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {
    private UserSessionManager sessionManager;
    private Switch adminModeSwitch;
    private TextView addArticleTextView, addProductTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        sessionManager = new UserSessionManager(this);

        TextView accountManagementTextView = findViewById(R.id.account_management_text_view);
        TextView logoutTextView = findViewById(R.id.logout_text_view);
        adminModeSwitch = findViewById(R.id.admin_mode_switch);
        addArticleTextView = findViewById(R.id.add_article_text_view);
        addProductTextView = findViewById(R.id.add_product_text_view);

        // 设置管理员模式开关默认关闭
        adminModeSwitch.setChecked(false);

        accountManagementTextView.setOnClickListener(v -> {
            Intent intent = new Intent(SettingsActivity.this, AccountManagementActivity.class);
            startActivity(intent);
        });

        logoutTextView.setOnClickListener(v -> {
            sessionManager.logoutUser();
            Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        });

        adminModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                addArticleTextView.setVisibility(View.VISIBLE);
                addProductTextView.setVisibility(View.VISIBLE);
                // 通知 MainActivity 开启管理员模式
                Intent intent = new Intent();
                intent.putExtra("admin_mode", true);
                setResult(RESULT_OK, intent);
            } else {
                addArticleTextView.setVisibility(View.GONE);
                addProductTextView.setVisibility(View.GONE);
                // 通知 MainActivity 关闭管理员模式
                Intent intent = new Intent();
                intent.putExtra("admin_mode", false);
                setResult(RESULT_OK, intent);
            }
        });


        addProductTextView.setOnClickListener(v -> {
            Intent intent = new Intent(SettingsActivity.this, AddProductActivity.class);
            startActivity(intent);
        });

        addArticleTextView.setOnClickListener(v -> {
            Intent intent = new Intent(SettingsActivity.this, AddArticleActivity.class);
            startActivityForResult(intent,1); // 使用startActivityForResult以便接收返回的数据
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
            // 将从AddArticleActivity返回的文章数据传递给MainActivity
            Article newArticle = data.getParcelableExtra("new_article");
            if (newArticle != null) {
                Intent resultIntent = new Intent();
                resultIntent.putExtra("new_article", newArticle);
                setResult(RESULT_FIRST_USER, resultIntent); // 使用不同的resultCode区分
                finish();
            }
        }
    }
}